package com.xyy.saas.payment.util;

import com.xyy.saas.payment.api.impl.PaymentCommonApiImpl;
import com.xyy.saas.payment.common.RocketMqTopic;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 从apollo中获取配置的属性的值
 * @Author: wuqiongda
 * @Date: 2019/11/12 19:27
 */
@Component
public class ApolloUtil  implements EnvironmentAware {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApolloUtil.class);

    static Environment environment;

    public static final String EMPTY_STRING = "";

    @Override
    public void setEnvironment(Environment environment) {
        ApolloUtil.environment = environment;
    }

    public static Environment getEnvironment() {
        return environment;
    }

    /**
     * 获取配置值, 如果不存在会返回空串
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return environment.getProperty(key, EMPTY_STRING);
    }

    /**
     * 获取Integer返回值
     * @param key
     * @return
     */
    public static Integer getIntegerProperty(String key) {
        return Integer.parseInt(environment.getProperty(key, EMPTY_STRING));
    }

    public static Integer getIntegerProperty(String key, Integer defaultValue) {
        String val = environment.getProperty(key, EMPTY_STRING);
        if (StringUtils.isBlank(val)) {
            return defaultValue;
        }
        return Integer.parseInt(val);
    }

    /**
     * 获取配置值, 如果不存在会返回传入的默认值
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue){
        return environment.getProperty(key, defaultValue);
    }

    /**
     * 获取配置值, 如果不存在会返回NULL
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public static<T> T getProperty(String key, Class<T> clazz){
        return environment.getProperty(key, clazz, null);
    }

    /**
     * 获取配置值, 如果不存在会返回传入默认值
     * @param key
     * @param clazz
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static<T> T getProperty(String key, Class<T> clazz, T defaultValue){
        return environment.getProperty(key, clazz, defaultValue);
    }

    public static String getPaySuccessNotifyTopic(String businessType, String businessOrderType) {
        try {
            // 1.根据产品线获取topic
            String topic = ApolloUtil.getProperty("paySuccess_notify_topic_" + businessOrderType);

            if(!StringUtils.isBlank(topic)) {
                return topic;
            }

            // 2.根据业务线获取topic
            topic = ApolloUtil.getProperty("paySuccess_notify_topic_" + businessType);

            if(!StringUtils.isBlank(topic)) {
                return topic;
            }
        }
        catch(Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }

        return RocketMqTopic.TOPIC_CALLBACK;
    }


}
